package com.masterclient.app.businessmodule.minemodule.message.contract;

import com.eoner.baselib.enums.FCPageViewState;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.managerlibrary.netsign.NetEnvManager;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.eoner.baselibrary.bean.personal.CouponMsgBean;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
* Created by TMVPHelper on 2018/05/22
*/
public class CounponMsgPresenter extends CounponMsgContract.Presenter{

    public List<CouponMsgBean.DataBean.ShItemsBean> list = new ArrayList<>();

    @Override
    public void getSalesruleMessage(String page, String page_size,boolean isRefresh) {
        if (list.size() == 0 && !isRefresh) {
            mView.baseShowPageLoading(false);
        }
        Observable<CouponMsgBean> observable = RetrofitUtil.getInstance().getSalesruleMessage(NetEnvManager.BASE_SURL+"1/customer/get_salesrule_message/"+page+"-"+page_size+".html");

        Disposable disposable = toSubscribe(observable, new Consumer<CouponMsgBean>() {
            @Override
            public void accept(@NonNull CouponMsgBean bean) throws Exception {
                mView.baseHiddenPageLoading();
                mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
                if("0".equals(bean.getCode())){
                    mView.showData(bean.getData(),page);
                } else {
                    mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR);
                    mView.showFailed();
                }
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.baseHiddenPageLoading();
                mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR);
                mView.showFailed();
            }
        });

        mRxManager.add(disposable);
    }
}